<?php

/**
 * Benchmark klasse
 *
 * @package		CMS
 * @author		Ruben
 * @since		14 November 2004
 * @version		1.0
 *
 */

class benchmark
{
	public $bFetched	= false;
	public $sName;
	private $iCal_time;
	private	$aMarks		= Array();
	private static $oRootbench;
	private static $iStart_time;
	
	public function __construct ()
	{
		$this->start_mark();

		if (function_exists('generate_str'))
		{
			$this->sKey = generate_str(10);
		}
		if (is_null(self::$iStart_time))	self::$iStart_time = Time::get_microtime();
		if (!is_null(self::$oRootbench))	self::$oRootbench->add_group('__unknown_mark', $this);
	}
	
	
	public function set_rootbench ()
	{
		self::$oRootbench	= $this;
		$this->bFetched		= true;
	}
	
	
	/**
	 * Voegt een nieuwe markering in
	 * @param	string		$sMark_name		Naam van de markering
	 */
	public function start_mark ()
	{
		$this->add_mark('__empty_mark');
	}
	
	/**
	 * Voegt een nieuwe markering in
	 * @param	string		$sMark_name		Naam van de markering
	 */
	public function add_mark ($sMark_name)
	{
		$this->aMarks[]	= Array($sMark_name, Time::get_microtime());
	}
	
	
	public function add_group ($sMark_name, &$oChild)
	{
		if (get_class($oChild) == __CLASS__)
		{
			if ($sMark_name != '__unknown_mark')
			{
				$oChild->bFetched	= true;
				$oChild->sName		= $sMark_name;
			}
			$this->aMarks[]	= Array($sMark_name, Time::get_microtime(), &$oChild);
		}
		return $oChild;
	}
	
	
	public function get_parsetime ()
	{
		$aLast	= end($this->aMarks);
		$aFirst	= reset($this->aMarks);
		return $aLast[1] - $aFirst[1];
	}
	
	
	private function get_realtime ()
	{
		return Time::get_microtime() - self::$iStart_time;
	}
	
	
	public function get_calculated_time ()
	{
		return $this->iCal_time;
	}
	
	
	/**
	 * Geeft de tijdsmetingen terug in tabel vorm
	 *
	 * @return	string		HTML-code met daarin de resultaten van de metingen
	 */
	public function get_results ($bShow_totaltime = false)
	{
		$sOutput			= '<table class="benchmark">';
		$this->iCal_time	= 0;
		$iTotaltime			= 0;
		$iMarks				= count($this->aMarks);
		$aLast_mark			= array_shift($this->aMarks);
		
		if ($iMarks < 2)
		{
			$this->add_mark('');
			$iMarks = count($this->aMarks);
		}
		
		foreach ($this->aMarks as $i => $aMark)
		{
			//find out what the name of this mark is
			if ($aMark[0] == '__empty_mark')		continue;
			if ($aMark[0] == '__unknown_mark')
			{
				$aMark[0] = 'unknown';
				if ($aMark[2]->bFetched)			continue;
				if (isset($aMark[2]) && $aMark[2]->sName != '')
				{
					$aMark[0] = $aMark[2]->sName;
				}
			}
			
			$sChild		 = '';
			//$iTime		 = $iTime2 = $aMark[1] - $aLast_mark[1];
			$iTime		 = $aMark[1] - $aLast_mark[1];
			
			if (isset($aMark[2]) && get_class($aMark[2]) == __CLASS__)
			{
				$sChild	= $aMark[2]->get_results();
				$iTime	= $aMark[2]->get_calculated_time();
				//$iTime2	= $aMark[2]->get_parsetime();
			}
			
			$this->iCal_time += $iTime;
			//$iTotaltime	+= $iTime2;
			$sClass		 = 'time'.($iTime > 0.006 ? ' notify' : '');
			
			$sOutput	.= '<tr><td>'.$aMark[0].'</td>';
			$sOutput	.= '<td>'.$sChild.'</td>';
			$sOutput	.= '<td class="'.$sClass.'">'.number_format($iTime, 6).'</td></tr>';
			
			$aLast_mark	= $aMark;
		}
		
		if ($bShow_totaltime)
		{
			$sOutput .= '<tfoot>';
			//$sOutput .= '<tr class="total"><td>calculated time3</td><td></td><td class="time">'.number_format($iTotaltime, 6).'</td></tr>';
			$sOutput .= '<tr class="total"><td>calculated time</td><td></td><td class="time">'.number_format($this->iCal_time, 6).'</td></tr>';
			$sOutput .= '<tr class="total"><td>measured time</td><td></td><td class="time">'.number_format($this->get_parsetime(), 6).'</td></tr>';
			$sOutput .= '<tr class="total"><td>real time</td><td></td><td class="time">'.number_format($this->get_realtime(), 6).'</td></tr>';
			$sOutput .= '</tfoot>';
		}
		
		$sOutput .= '</table>';
		
		return $sOutput;
	}
}
?>